﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 养老金支付系统
{
    public partial class FormSBBFCX : FormBase
    {
        public FormSBBFCX()
        {
            InitializeComponent();
        }

        string bfqh = string.Empty;
        const string sql1 = @"select 补发期号,个人编号,姓名,身份证号,交易期号,补发笔数,补发金额 from 查失败补发明细 where 补发期号=@BFQH
union select 补发期号,'合计',cast(count(*) as nvarchar(4))+'人','','',sum(补发笔数),sum(补发金额) from 查失败补发明细 
group by 补发期号 having 补发期号=@BFQH";
        const string sql2 = @"select 补发期号,个人编号,姓名,身份证号,交易期号,补发笔数,补发金额 from 查一次性支付失败补发明细 where 补发期号=@BFQH
union select 补发期号,'合计',cast(count(*) as nvarchar(4))+'人','','',sum(补发笔数),sum(补发金额) from 查一次性支付失败补发明细 
group by 补发期号 having 补发期号=@BFQH";
        
        SaveFileDialog sfd = new SaveFileDialog() { Title = "导出", Filter = "Excel(97-2003)|*.xls" };

        void btnEnabled( bool b)
        {
            button1.Enabled = b;
            button2.Enabled = b;
            button3.Enabled = b;
        }

        void init()
        {
            this.Text = "交易失败补发明细查询";
            textBox1.Text = SqlHelper.getScalar("select dbo.GetNextQH(dbo.GetNowQH())") as string;
            comboBox1.SelectedIndex = 0;
        }

        void find()
        {
            if (this.bfqh.Length == 6)
            {
                if (comboBox1.SelectedIndex == 0)
                    dataGridView1.DataSource = SqlHelper.getDataTable(sql1, new SqlParameter("@BFQH", this.bfqh));
                else
                    dataGridView1.DataSource = SqlHelper.getDataTable(sql2, new SqlParameter("@BFQH", this.bfqh));
            }
        }

        void output()
        {
            DataTable dt = dataGridView1.DataSource as DataTable;
            if (dt != null)
            {
                sfd.FileName = "第" + this.bfqh + "期" + comboBox1.Text + "交易失败补发明细.xls";
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    ExcelHelper.DataTableToExcel(dt, sfd.FileName);
                }
            }
        }

        void print()
        {
            FormPrintView fpv = new FormPrintView()
            {
                ReportDataSetName = "失败补发明细",
                DataSources = dataGridView1.DataSource,
                ReportParam = new Microsoft.Reporting.WinForms.ReportParameter[] { 
                    new Microsoft.Reporting.WinForms.ReportParameter("bfqh", this.bfqh), 
                    new Microsoft.Reporting.WinForms.ReportParameter("bflx",comboBox1.Text) 
                },
                ReportPath = "SBBFMX.rdlc"
            };
            fpv.ShowDialog();
        }

        private void FormSBBFCX_Load(object sender, EventArgs e)
        {
            init();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            this.bfqh = textBox1.Text;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            btnEnabled(false);
            find();
            btnEnabled(true);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            btnEnabled(false);
            output();
            btnEnabled(true);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            btnEnabled(false);
            print();
            btnEnabled(true);
        }
    }
}
